package com.asynctm.android.socialnetwork.activity;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.asynctm.android.R;
import com.asynctm.android.socialnetwork.dto.ContactDto;

/**
 * Adapter para la lista de contactos
 * 
 * @author rockdrigo
 */
public class ContactListAdapter extends BaseAdapter {

	private List<ContactDto> contacts;
	
	private Context context;
	
	private LayoutInflater layoutInflater;
	
	public ContactListAdapter(Context context, List<ContactDto> contacts) {
		this.context = context;
		this.contacts = contacts;
		this.layoutInflater = LayoutInflater.from(this.context);
	}
	
	public int getCount() {
		return contacts.size();
	}

	public Object getItem(int location) {
		return contacts.get(location);
	}

	public long getItemId(int location) {
		return location;
	}

	public View getView(int location, View convertView, ViewGroup viewGroup) {
		ContactListItemViewHolder contactListItemViewHolder = null;
		ContactDto contact = contacts.get(location);
		View v = convertView;
		if(v == null) {
			v = layoutInflater.inflate(R.layout.contact_list_item, null);
			contactListItemViewHolder = new ContactListItemViewHolder(v);
			v.setTag(contactListItemViewHolder);
		} else {
			contactListItemViewHolder = (ContactListItemViewHolder) v.getTag();
		}
		contactListItemViewHolder.populate(contact);
		return v;
	}

}
